package com.data.center.config;

import com.data.center.annotation.ForeignInfo;
import com.data.center.util.ForeignManage.ForeignManageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.type.filter.AssignableTypeFilter;

import java.lang.reflect.Field;
import java.util.Set;

@Configuration
@Slf4j
public class AnnotationScanConfig {

    @Bean
    public ForeignManageUtils processForeignAnnotation() throws ClassNotFoundException, NoSuchMethodException {
        log.info(">>>>>>>>>> 开始扫描外键注解 <<<<<<<<<<");
        // 初始化外键关系管理工具类
        ForeignManageUtils foreignManageUtils = new ForeignManageUtils();// 获取外键关系查询管理工具类bean
        foreignManageUtils.init();
        // 扫描包含实体类的包
        String basePackage = "com.data.center.entity";
        // 获取包下所有类
        ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
        scanner.addIncludeFilter(new AssignableTypeFilter(Object.class));
        Set<BeanDefinition> beans = scanner.findCandidateComponents(basePackage);
        log.info("共扫描到需要处理的类：" + beans.size() + "个");
        for (BeanDefinition bd : beans) {
            Class<?> clz = Class.forName(bd.getBeanClassName());
            log.info("扫描类：" + clz.getName());
            // 检查类中的每个字段
            for (Field field : clz.getDeclaredFields()) {
                ForeignInfo annotation = field.getAnnotation(ForeignInfo.class);
                if (annotation != null) {
                    // 获取注解配置
                    Class<?> targetClass = annotation.targetClass();
                    String foreignKey = annotation.foreignKey();
                    Class<?> serviceClass = annotation.serviceClass();

                    // 输出关系
                    log.info("获取到外键关系：" + " name " + targetClass.getName()
                            + ", foreign key " + foreignKey + "serviceName" + serviceClass.getName()
                            + "searchMethodName" + annotation.searchByForeignKeyMethodName()
                    );
                    // 添加到关系管理工具中
                    foreignManageUtils.addForeignDataItem(
                            clz.getName(),
                            field.getName(),
                            targetClass.getName(),
                            foreignKey,
                            serviceClass,
                            serviceClass.getMethod(annotation.searchByForeignKeyMethodName(), String.class),
                            serviceClass.getMethod(annotation.deleteByForeignKeyMethodName(), String.class),
                            serviceClass.getMethod(annotation.mergeByForeignKeyMethodName(), String.class, String.class)
                    );
                }
            }
        }

        log.info(">>>>>>>>>> 结束扫描外键注解，共注册" + ForeignManageUtils.foreignDataItemList.size() + "个外键关系 <<<<<<<<<<");
        return foreignManageUtils;
    }

}